#!/usr/bin/python
import os, fnmatch, sys

root = '/home/guohui/ipc5'

def __compare( x, y ):
    if len( x ) == len( y ):
        return cmp( x, y )
    else:
        return cmp( len( x ), len( y ) )

def all_files( root, patterns = '*', single_level = False, yield_folders = False ):
    patterns = patterns.split( ';' )
    for path, subdirs, files in os.walk( root ):
        if yield_folders:
            files.extend( subdirs )
        files.sort( __compare )
        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch( name, pattern ):
                    yield os.path.join( path, name )
                    break
        if single_level:
            break

if __name__ == '__main__':
	#print root
	for problem_name in all_files( root, patterns = 'p[0-9][0-9].pddl;P[0-9][0-9]_[!D]*.PDDL;P[0-9][0-9].PDDL;pfile[0-9]*;problem.pddl' ):
		#print problem_name
		dirname, basename = os.path.split( problem_name )
#for IPC5
		domain_name = os.path.join( dirname, "domain.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "domain_"+basename )
#for IPC4
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, basename[:3]+"_DOMAIN.PDDL" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "DOMAIN.PDDL" )
#for IPC3
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "Settlers.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "Settlers.pddl.untyped" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "UMT.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "free.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "adlSat.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "StripsRover.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "Depots.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "driverlog.pddl" )
		if not os.path.exists( domain_name ):
			domain_name = os.path.join( dirname, "zenotravelStrips.pddl" )
		if not os.path.exists( domain_name ):
			raise SystemExit( "Error: Could not find domain file - yao" )

		print "/home/guohui/downward/solve fF", domain_name, problem_name


